package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.beans.Login;
import br.ucb.sigplan.beans.service.BeansUtil;
import br.ucb.sigplan.validator.Validator;

public class LoginAction implements Action {

	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<String> erros = Validator.validaLogin(req);
		
		if (erros.isEmpty()) {
			Login login = new Login();
			BeansUtil.populateLogin(login, req);
			req.getSession().setAttribute("login", login);
			if (login.getTipo().getValue() == 'd') {
				req.setAttribute("msg", "bemVindoDiretor");
			}else {
				req.setAttribute("msg", "bemVindoProfessor");
			}
			return "/jsp/index.jsp";
		}
		else {
			req.setAttribute("erros", erros);
			return "/login.jsp";
		}
	}

}
